﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using SoftFX.Basic;
using SoftFX.Extended;

namespace Mql2Fdk
{
	/// <summary>
	/// Provides methods to initialize, start and stop an adviser.
	/// </summary>
	public class MqlLauncher : IDisposable
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="address"></param>
		/// <param name="username"></param>
		/// <param name="password"></param>
		/// <param name="location"></param>
		/// <param name="symbol"></param>
		/// <param name="priceType"></param>
		/// <param name="periodicity"></param>
		/// <param name="adapter"></param>
		public MqlLauncher(string address, string username, string password, string location, string symbol, PriceType priceType, BarPeriod periodicity, MqlAdapter adapter)
		{
			if (null == address)
			{
				throw new ArgumentNullException("address");
			}
			if (null == username)
			{
				throw new ArgumentNullException("username");
			}
			if (null == password)
			{
				throw new ArgumentNullException("password");
			}
			if (null == periodicity)
			{
				throw new ArgumentNullException("periodicity");
			}
			if (null == adapter)
			{
				throw new ArgumentNullException("adapter");
			}
			if ((PriceType.Bid != priceType) && (PriceType.Ask != priceType))
			{
				throw new ArgumentOutOfRangeException("priceType", priceType, "Expected: PriceType.Bid or PriceType.Ask");
			}

			m_adapter = adapter;

			string logsLocation = string.Empty;

			if (null == location)
			{
				location = Assembly.GetExecutingAssembly().Location;
				location = Path.GetDirectoryName(location);
				location = Path.Combine(location, "Advisers");
				Directory.CreateDirectory(location);
				location = Path.Combine(location, address);
				Directory.CreateDirectory(location);
				location = Path.Combine(location, username);
				Directory.CreateDirectory(location);
			}
			else if (!string.IsNullOrEmpty(location))
			{
				logsLocation = Path.Combine(location, "Logs");
				Directory.CreateDirectory(logsLocation);
			}
			FixConnectionStrings conns = new FixConnectionStrings(address, username, password, logsLocation);

			m_manager = new Manager(conns.TradeConnectionString, conns.FeedConnectionString, location);
			m_adapter.Initialize(m_manager, symbol, priceType, periodicity);
		}

		/// <summary>
		/// Starts adviser.
		/// </summary>
		public void Start()
		{
			m_manager.Start();
			m_adapter.Start();
		}
		/// <summary>
		/// Stops adviser.
		/// </summary>
		public void Stop()
		{
			m_adapter.Stop();
			m_manager.Stop();
		}
		/// <summary>
		/// Closes all connections and flushes all data.
		/// </summary>
		public void Dispose()
		{
			Manager manager = m_manager;
			if (null != manager)
			{
				manager.Dispose();
			}
		}
		#region members
		private MqlAdapter m_adapter;
		private Manager m_manager;
		#endregion
	}
}
